package com.example.dqnm.service.impl;

import com.example.dqnm.mapper.ProductsDao;
import com.example.dqnm.mapper.ProducttypeDao;
import com.example.dqnm.pojo.*;
import com.example.dqnm.service.categoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class categoryServiceImpl implements categoryService {
    @Autowired
    private ProductsDao productsDao;
    @Autowired
    private ProducttypeDao producttypeDao;
    @Override
    public List<ProductsList> findAllcategory() {
        List<ProductsList> productsLists = new ArrayList<>();
        ProducttypeExample producttypeExample = new ProducttypeExample();
        List<Producttype> producttypes = producttypeDao.selectByExample(producttypeExample);
        for (int i = 0; i < producttypes.size(); i++) {
            Producttype producttype = producttypes.get(i);
            ProductsExample productsExample = new ProductsExample();
            ProductsExample.Criteria criteria = productsExample.createCriteria();
            /*相同类型的产品*/
            criteria.andTypenameEqualTo(producttype.getName());
            /*需要上架的产品*/
            criteria.andStatusEqualTo("1");
            List<Products> products = productsDao.selectByExample(productsExample);
            ProductsList productsList = new ProductsList(producttype, products);
            productsLists.add(productsList);
        }
        return productsLists;
    }
}
